<template>
  <el-dialog v-model="dialogVisible" title="重置密码" width="500px" draggable>
    <el-form ref="userFormRef" :model="form" :rules="rules">
      <el-form-item label="密码" prop="userPasswd">
        <el-input placeholder="请输入密码" type="password" v-model="form.userPasswd" show-password></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="_colse">取消</el-button>
        <el-button type="primary" @click="_submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ResetPsdDrawer">
import { ref, reactive } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { resetUserPassWord } from "@/api/modules/user";
const dialogVisible = ref(false);
const form = reactive({ userPasswd: "", userList: [{ userId: "" }] });
const userFormRef = ref<FormInstance>();
const rules = reactive({
  userPasswd: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur"
    },
    {
      //插入正则验证：大小写、数字、至少6位、不常用字符
      pattern: /^((?=.*[a-zA-Z])(?=.*\d))*((?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+]))*[a-zA-Z\d!@#$%^&*()_+]{6,18}$/,
      message: "密码6-18位(含数字、字母、特殊字符_-)",
      trigger: "blur"
    }
  ]
});
const openDialog = (userId: string) => {
  form.userList = [{ userId }];
  dialogVisible.value = true;
};
/**
 * 关闭弹窗
 */
const _colse = () => {
  dialogVisible.value = false;
  userFormRef.value && userFormRef.value.resetFields();
};
/**
 * 提交修改信息
 */
const _submit = async (): Promise<void> => {
  userFormRef.value!.validate(async valid => {
    if (!valid) return;
    await resetUserPassWord(form);
    ElMessage({
      message: "重置成功",
      type: "success"
    });
    dialogVisible.value = false;
    userFormRef.value && userFormRef.value.resetFields();
  });
};
defineExpose({ openDialog });
</script>
